from .basehandler import require_login, BaseHandler
from . import models, forms
import re
import datetime


class DefaultHandler(BaseHandler):

    _template_dir = 'mine'

    @require_login
    def oninit(self):
        super().oninit()

        if self.is_get:
            self.user.avatar = self.config.DEFAULT_AVATAR
            self.assign('user', self.user)
            self.assign('menu', self.config.mineLeftMenu)

    def index(self):
        return self.redirect('article')

    def setting(self):
        if self.is_get:
            form = forms.PersonInfoForm(instance=self.user)
            form.fields['username'].widget.attrs['readonly'] = True
            return self.render(form=form)
        else:
            form = forms.PersonInfoForm(self.request.POST, instance=self.user)
            if form.is_valid():
                form.save()
            return self.STAY

    def collection(self):
        return self.render()

    def comments(self):
        return self.render()

    def article(self):
        search = self.get_arg('search', None)
        articles = models.Article.objects.filter(author=self.user)
        if search:
            articles = articles.filter(title__icontains=search)
        return self.render(articles=articles)

    def follow(self):
        users = models.Follow.objects.filter(follower=self.user)
        users = (item.target for item in users)
        return self.render(users=users)

    def fans(self):
        users = models.Follow.objects.filter(target=self.user)
        users = (item.follower for item in users)
        return self.render(users=users)

    def publish(self):
        if self.is_get:
            return self.render()
        else:
            data = self._get_article_args()
            data.author = self.user
            models.Article.objects.create(**data)
            self.user.article_count += 1
            self.user.save()
            return self.index()

    def article_edit(self):
        article_id = self.get_arg('id')
        if self.is_get:
            article = models.Article.objects.filter(id=article_id)
            if not article:
                return '文章不存在'
            data = article.first()
            return self.render(data=data)
        else:
            data = self._get_article_args()
            models.Article.objects.filter(id=article_id).update(**data)
            return self.index()

    def article_del(self):
        article = models.Article.objects.filter(id=self.get_arg('id')).first()
        if article:
            article.delete()
            self.user.article_count -= 1
            self.user.save()
            return self.index()

    def _get_article_args(self):
        data = self.get_args(('title', 'tags', 'content', 'description', 'status'))
        data.title = data.title.strip()
        data.tags = re.sub(' *, *', ',', data.tags)
        if data.tags:
            for tag in data.tags.split(','):
                models.ArticleTag.objects.get_or_create(name=tag)
        return data